import { get, post, put, del, upload } from '@/utils/request'

// 用户相关API
export const userApi = {
  // 用户登录
  login: (data) => post('/auth/login', data),
  
  // 用户注册
  register: (data) => post('/auth/register', data),
  
  // 用户登出
  logout: () => post('/user/logout'),
  
  // 获取用户信息
  getUserInfo: () => get('/user/info'),
  
  // 更新用户信息
  updateUserInfo: (data) => put('/user/update', data),
  
  // 修改密码
  changePassword: (data) => put('/user/password', data),
  
  // 上传头像
  uploadAvatar: (file) => upload('/user/avatar', file),
  
  // 检查认证状态
  checkAuth: () => get('/user/auth')
}

// 商品相关API
export const productApi = {
  // 获取商品列表
  getProducts: (params) => get('/products', params),
  
  // 获取商品详情
  getProductDetail: (id) => get(`/products/${id}`),
  
  // 获取商品分类
  getCategories: () => get('/categories'),
  
  // 搜索商品
  searchProducts: (params) => get('/search', params),
  
  // 获取热门商品
  getHotProducts: () => get('/products/hot'),
  
  // 获取推荐商品
  getRecommendProducts: () => get('/products/recommend'),
  
  // 获取商品评价
  getProductReviews: (productId, params) => get(`/products/${productId}/reviews`, params),
  
  // 添加商品评价
  addProductReview: (productId, data) => post(`/products/${productId}/reviews`, data)
}

// 购物车相关API
export const cartApi = {
  // 获取购物车
  getCart: () => get('/cart'),
  
  // 添加商品到购物车
  addToCart: (data) => post('/cart/add', data),
  
  // 更新购物车商品数量
  updateCartItem: (itemId, data) => put(`/cart/items/${itemId}`, data),
  
  // 删除购物车商品
  removeFromCart: (itemId) => del(`/cart/items/${itemId}`),
  
  // 清空购物车
  clearCart: () => del('/cart'),
  
  // 批量删除购物车商品
  batchRemoveFromCart: (itemIds) => post('/cart/batch-remove', { itemIds })
}

// 订单相关API
export const orderApi = {
  // 创建订单
  createOrder: (data) => post('/orders', data),
  
  // 获取订单列表
  getOrders: (params) => get('/orders', params),
  
  // 获取订单详情
  getOrderDetail: (orderId) => get(`/orders/${orderId}`),
  
  // 取消订单
  cancelOrder: (orderId) => put(`/orders/${orderId}/cancel`),
  
  // 确认收货
  confirmOrder: (orderId) => put(`/orders/${orderId}/confirm`),
  
  // 申请退款
  applyRefund: (orderId, data) => post(`/orders/${orderId}/refund`, data),
  
  // 获取订单状态
  getOrderStatus: (orderId) => get(`/orders/${orderId}/status`)
}

// 支付相关API
export const paymentApi = {
  // 创建支付订单
  createPayment: (orderId, data) => post(`/payments/${orderId}`, data),
  
  // 获取支付状态
  getPaymentStatus: (paymentId) => get(`/payments/${paymentId}/status`),
  
  // 支付回调
  paymentCallback: (data) => post('/payments/callback', data)
}

// 地址相关API
export const addressApi = {
  // 获取地址列表
  getAddresses: () => get('/addresses'),
  
  // 添加地址
  addAddress: (data) => post('/addresses', data),
  
  // 更新地址
  updateAddress: (addressId, data) => put(`/addresses/${addressId}`, data),
  
  // 删除地址
  deleteAddress: (addressId) => del(`/addresses/${addressId}`),
  
  // 设置默认地址
  setDefaultAddress: (addressId) => put(`/addresses/${addressId}/default`)
}

// 收藏相关API
export const favoriteApi = {
  // 获取收藏列表
  getFavorites: () => get('/favorites'),
  
  // 添加收藏
  addFavorite: (productId) => post('/favorites', { productId }),
  
  // 取消收藏
  removeFavorite: (productId) => del(`/favorites/${productId}`),
  
  // 检查是否已收藏
  checkFavorite: (productId) => get(`/favorites/check/${productId}`)
}

// 优惠券相关API
export const couponApi = {
  // 获取可用优惠券
  getAvailableCoupons: () => get('/coupons/available'),
  
  // 获取我的优惠券
  getMyCoupons: (params) => get('/coupons/my', params),
  
  // 领取优惠券
  claimCoupon: (couponId) => post(`/coupons/${couponId}/claim`),
  
  // 使用优惠券
  useCoupon: (couponId, orderId) => post(`/coupons/${couponId}/use`, { orderId })
}

// 系统相关API
export const systemApi = {
  // 获取系统配置
  getConfig: () => get('/system/config'),
  
  // 获取轮播图
  getBanners: () => get('/system/banners'),
  
  // 获取公告
  getAnnouncements: () => get('/system/announcements'),
  
  // 获取帮助信息
  getHelp: () => get('/system/help'),
  
  // 提交反馈
  submitFeedback: (data) => post('/system/feedback', data),
  
  // 获取版本信息
  getVersion: () => get('/system/version')
}

// 统计相关API
export const statsApi = {
  // 获取用户统计
  getUserStats: () => get('/stats/user'),
  
  // 获取订单统计
  getOrderStats: (params) => get('/stats/orders', params),
  
  // 获取商品统计
  getProductStats: (params) => get('/stats/products', params)
}

// 导出所有API
export default {
  user: userApi,
  product: productApi,
  cart: cartApi,
  order: orderApi,
  payment: paymentApi,
  address: addressApi,
  favorite: favoriteApi,
  coupon: couponApi,
  system: systemApi,
  stats: statsApi
} 